package com.hualan.sfzy.controller.ssh;

import com.hualan.sfzy.model.entity.PsychologyTestResult;
import com.hualan.sfzy.model.entity.PsychologyTestTitle;
import com.hualan.sfzy.service.impl.PsychologyTestResultServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/psychologyTestResult")
public class PsychologyTestResultController {
    @Autowired
    private PsychologyTestResultServiceImpl Service;
    /*添加*/
    @PostMapping("/add")
    public Result add(@RequestBody PsychologyTestResult entity) {
        int i = Service.add(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*编辑*/
    @PutMapping("/edit")
    public Result edit(@RequestBody PsychologyTestResult entity) {
        int i = Service.edit(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        int i = Service.remove(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }
    /*批量删除*/
    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestParam("ids") Integer[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请提供有效的删除 ID 列表");
        }
        int i = Service.removeBatch(ids);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error("未删除任何记录");
    }
    /*查询所有*/
    @GetMapping("/find")
    public Result find() {
        List<PsychologyTestResult> list = Service.find();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }

    /*分页查询所有*/
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                           PsychologyTestResult entity) {
        Map<String, Object> map = Service.findPage(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }
    /*根据id查询单个*/
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTestResult psychologyTestResult = Service.findById(id);
        if (psychologyTestResult != null) {
            return Result.success(psychologyTestResult);
        }
        return Result.error("未找到ID为" + id + "的记录");
    }
    /*根据id查询单个(多表)*/
    @GetMapping("/findByIdDuo/{id}")
    public Result findByIdDuo(@PathVariable("id") Integer id) {
        if (id == null || id <= 0) {
            return Result.error("ID参数不合法");
        }
        PsychologyTestResult psychologyTestResult = Service.findByIdDuo(id);
        if (psychologyTestResult != null) {
            return Result.success(psychologyTestResult);
        }
        return Result.error("未找到ID为" + id + "的多表记录");
    }
    /*根据多表全查(模糊)*/
    @GetMapping("/findByCondition")
    public Result findByCondition(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                  PsychologyTestResult entity

    ) {
        Map<String, Object> map = Service.findDuo(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }
    /**根据用户id查询测评结果*/
    @GetMapping("/selSysUserId")
    public Result selSysUserId(@RequestParam("userId") Integer userId,
                               @RequestParam("testId") Integer testId){
        List<PsychologyTestResult> list = Service.selSysUserId(userId,testId);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }
    /**根据用户id查询测评结果*/
    @GetMapping("/sellectByUserId/{userId}")
    public Result sellectByUserId(@PathVariable("userId") Integer userId){
        List<PsychologyTestResult> list = Service.sellectByUserId(userId);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }

}
